package top.yangbuyi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import top.yangbuyi.constants.Constants;
import top.yangbuyi.domain.CareHistory;
import top.yangbuyi.domain.CareOrder;
import top.yangbuyi.domain.CareOrderItem;
import top.yangbuyi.dto.CareHistoryDto;
import top.yangbuyi.mapper.CareHistoryMapper;
import top.yangbuyi.mapper.CareOrderItemMapper;
import top.yangbuyi.mapper.CareOrderMapper;
import top.yangbuyi.service.CareService;
import top.yangbuyi.utils.IdGeneratorSnowflake;
import top.yangbuyi.utils.StringUtils;

import java.util.List;

/**
 * @ClassName: CareServiceImpl
 * @Author: campsis-tk
 * @Date: 2020/9/15 16:19
 * @Description:
 **/
@Service
public class CareServiceImpl implements CareService {

	  @Autowired
	  private CareHistoryMapper careHistoryMapper;
	  @Autowired
	  private CareOrderMapper careOrderMapper;
	  @Autowired
	  private CareOrderItemMapper careOrderItemMapper;

	  /**
	   * @功能描述:根据患者ID查询---病例信息
	   * @Description:
	   * @Author: TeouBle
	   * @Date: 2020/9/16 0:05
	   */
	  @Override
	  public List<CareHistory> queryCareHistoryByPatientId(String patientId) {
			QueryWrapper<CareHistory> wrapper = new QueryWrapper<>();
			wrapper.eq(CareHistory.COL_PATIENT_ID, patientId);
			List<CareHistory> careHistories = this.careHistoryMapper.selectList(wrapper);
			System.err.println(careHistories);
			return careHistories;
	  }

	  /**
	   * @param chId 病例ID
	   * @功能描述:根据病例ID查询---处方信息
	   * @Description:
	   * @Author: TeouBle
	   * @Date: 2020/9/16 0:04
	   */
	  @Override
	  public List<CareOrder> queryCareOrdersByChId(String chId) {
			QueryWrapper<CareOrder> wrapper = new QueryWrapper<>();
			wrapper.eq(CareOrder.COL_CH_ID, chId);
			return this.careOrderMapper.selectList(wrapper);
	  }

	  /**
	   * @param coId
	   * @功能描述:根据处方ID---查询处方详细信息
	   * @Description:
	   * @Author: TeouBle
	   * @Date: 2020/9/16 0:18
	   * @date: 9/17 修改 qw.eq(StringUtils.isNotBlank(status),CareOrderItem.COL_STATUS,status);//添加的
	   */
	  @Override
	  public List<CareOrderItem> queryCareOrdersItemsByCoId(String coId, String status) {
			QueryWrapper<CareOrderItem> wrapper = new QueryWrapper<>();
			wrapper.eq(CareOrderItem.COL_CO_ID, coId);
			wrapper.eq(StringUtils.isNotBlank(status), CareOrderItem.COL_STATUS, status);//添加的
			return this.careOrderItemMapper.selectList(wrapper);
	  }

	  /**
	   * @Description: 保存病例信息
	   * @Param: CareHistory
	   * @Return: careHistoryDto
	   * @Author: campsis-tk
	   * @Date: 2020/9/16
	   */
	  @Override
	  public CareHistory saveOrUpdateCareHistory(CareHistoryDto careHistoryDto) {
			CareHistory careHistory = new CareHistory();
			BeanUtil.copyProperties(careHistoryDto, careHistory);
			if (StringUtils.isNotBlank(careHistory.getChId())) {//修改
				  this.careHistoryMapper.updateById(careHistory);
			} else {//添加
				  careHistory.setChId(IdGeneratorSnowflake.generatorIdWithProfix(Constants.ID_PREFIX_CH));
				  this.careHistoryMapper.insert(careHistory);
			}
			return careHistory;
	  }

	  /**
	   * @param regId
	   * @功能描述:根据挂号ID查询病例信息
	   * @Description:
	   * @Author: TeouBle
	   * @Date: 2020/9/17 10:37
	   */
	  @Override
	  public CareHistory queryCareHistoryByRegId(String regId) {
			QueryWrapper<CareHistory> wrapper = new QueryWrapper<>();
			wrapper.eq(CareHistory.COL_REG_ID, regId);
			return this.careHistoryMapper.selectOne(wrapper);
	  }


}
